//
//  SiJiTypeView.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/10.
//

#import "SiJiTypeView.h"

@implementation SiJiTypeView
- (void)awakeFromNib{
   [super awakeFromNib];
   [self createCollectionView];
  
}
+ (instancetype)shareInstance {
    return [[NSBundle mainBundle] loadNibNamed:kClsID(SiJiTypeView) owner:nil options:nil][0];
}
-(void)createCollectionView{

    [self getCollectionViewHeight];
    UICollectionViewFlowLayout  * layOut =[[UICollectionViewFlowLayout alloc] init];
    layOut.minimumInteritemSpacing =0;
    layOut.minimumLineSpacing = 0;
    layOut.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-110.0,  _collectionViewHeight) collectionViewLayout:layOut];
    layOut.itemSize=CGSizeMake( (ScreenWidth-110.0)/3.0,  30);
    _collectionView.collectionViewLayout=layOut;
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    [_collectionView registerNib:kNib(ManYIDuCollectionViewCell) forCellWithReuseIdentifier:kClsID(ManYIDuCollectionViewCell)];
     [self.BgView addSubview:_collectionView];
}
#pragma mark----delegate、dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ManYIDuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kClsID(ManYIDuCollectionViewCell) forIndexPath:indexPath];
    cell.titleButton.userInteractionEnabled=NO;
    JueSeModel  *   model=self.imageArray[indexPath.row];
    [cell.titleButton setTitle:[NSString stringWithFormat:@" %@",model.value] forState:UIControlStateNormal];
   
    if (self.selectIndex==indexPath.row){
        [cell.titleButton setImage:[UIImage imageNamed:@"small_select_icon"] forState:UIControlStateNormal];
    } else {
        [cell.titleButton setImage:[UIImage imageNamed:@"small_unSelect_icon"] forState:UIControlStateNormal];
    }
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        UICollectionReusableView * headerV = (UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
        return headerV;
        
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

        UICollectionReusableView  * footerView=(UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collettionSectionFoot" forIndexPath:indexPath];
        return footerView;
    } else {
        
        return nil;
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat  photoWidth=(ScreenWidth-110.0)/3.0;
    CGSize size=CGSizeMake(photoWidth, 30);
    return size;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreenWidth, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    self.ToSelectIndex(indexPath.row);

}
-(void)setDataSource:(NSMutableArray *)dataSource{
    self.imageArray=dataSource;
    [self getCollectionViewHeight];
    [_collectionView reloadData];
}

-(void)getCollectionViewHeight{

   CGFloat photoWidth=30;
    _collectionViewHeight=0;
    _collectionViewHeight=_collectionViewHeight+20;
    NSInteger   number= self.imageArray.count%3;
    NSInteger   zomeNumber=self.imageArray.count/3;
    if (number== 0){

        number=zomeNumber;

    } else {

        number=zomeNumber+1;
    }
    _collectionViewHeight=_collectionViewHeight+(number*photoWidth);
    _collectionView.height=_collectionViewHeight;
    _BgView.height=_collectionViewHeight;
    [_collectionView reloadData];
}
-(void)currentSelectIndex:(NSInteger)selectIndex{
    self.selectIndex=selectIndex;
    [self.collectionView reloadData];
}
@end
